package ynu.edu.elmspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.elmspringboot.mapper.CartMapper;
import ynu.edu.elmspringboot.mapper.FoodMapper;
import ynu.edu.elmspringboot.mapper.BusinessMapper;
import ynu.edu.elmspringboot.po.Cart;
import ynu.edu.elmspringboot.po.Food;
import ynu.edu.elmspringboot.po.Business;
import ynu.edu.elmspringboot.service.CartService;

import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public Cart getCartWithFoodAndBusiness(Integer cartId) {
        Cart cart = cartMapper.selectById(cartId);
        if (cart != null) {
            Food food = foodMapper.selectById(cart.getFoodId());
            Business business = businessMapper.selectById(cart.getBusinessId());
            cart.setFood(food);
            cart.setBusiness(business);
        }
        return cart;
    }

    @Override
    public int removeCart(Cart cart) {
        return cartMapper.removeCart(cart);
    }

    @Override
    public List<Cart> listCart(String userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return cartMapper.selectList(queryWrapper);
    }


}